package com.zjyd.douyangcommondatasource.config;

import lombok.Data;
import org.dromara.autotable.springboot.EnableAutoTable;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * 数据源配置类
 * 用于配置MySQL数据库连接信息，并创建数据源Bean
 */
@Data
@Configuration
//启用自动建表扫描
@EnableAutoTable(basePackages = "com.zjyd.douyangcommoncore.po")
//扫描mapper
@MapperScan("com.zjyd.douyangcommoncore.mapper")
public class DatasourceConfig {

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    private String driverClassName = "com.mysql.cj.jdbc.Driver";

    /**
     * 创建并配置数据源Bean
     * 该方法使用MySQL数据库连接信息初始化DriverManagerDataSource实例
     * @return DataSource 配置好的数据源实例
     */
    @Bean
    @Primary
    public DataSource dataSource() {
        // 创建数据源实例并设置连接参数
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }
}

